﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Xiaozilang_Ordering_System
{

    public partial class Registration_interface : Form
    {
        public static int Judge = 0;
        public Registration_interface()
        {
            InitializeComponent();
            textBox1.Enter += new EventHandler(textBox1_Enter);
            textBox1.Leave += new EventHandler(textBox1_Leave);
            textBox2.Enter += new EventHandler(textBox2_Enter);
            textBox2.Leave += new EventHandler(textBox2_Leave);
            textBox3.Enter += new EventHandler(textBox3_Enter);
            textBox3.Leave += new EventHandler(textBox3_leave);
            textBox4.Enter += new EventHandler(textBox4_Enter);
            textBox4.Leave += new EventHandler(textBox4_Leave);
            textBox5.Enter += new EventHandler(textBox5_Enter);
            textBox5.Leave += new EventHandler(textBox5_Leave);
            textBox6.Enter += new EventHandler(textBox6_Enter);
            textBox6.Leave += new EventHandler(textBox6_Leave);
            textBox7.Enter += new EventHandler(textBox7_Enter);
            textBox7.Leave += new EventHandler(textBox7_Leave);
            textBox8.Enter += new EventHandler(textBox8_Enter);
            textBox8.Leave += new EventHandler(textBox8_Leave);

        }
        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox1_Leave(object sender, EventArgs e)
        {   
            if (textBox1.Text == "")
            {
                textBox1.Text = "手机号";
            }
            //判断手机号是否被注册过
            SqlDataReader reader1 = null;
            SqlDataReader reader2 = null;
            string tel1 = null;
            string tel2 = null;
            string sql1 = null;
            string sql2 = null;
            try
            {
                //判断该账户是否存在，同时查询两表
                    sql1 = "select * from User1 where UStel='" + textBox1.Text + "'";
                    reader1 = DBHelper.Select(sql1);
                    while (reader1.Read())
                    {
                        tel1 = reader1["UStel"].ToString();
                    }
                reader1.Close();
                sql2 = "select * from Business where BStel='" + textBox1.Text + "'";
                    reader2 = DBHelper.Select(sql2);
                while (reader2.Read())
                {
                    tel2 = reader2["BStel"].ToString();
                }
                reader2.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("程序异常，请联系管理员!\n原因：" + ex.Message);
            }
            //已注册时弹窗提醒
            if (tel1 == textBox1.Text||tel2==textBox1.Text)
            {
                MessageBox.Show("该用户已注册，请重新输入");
                textBox1.Text = "手机号";
            }
        }
        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "手机号")
            {
                textBox1.Text = "";
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox2_Enter(object sender, EventArgs e)
        {
            //用户选择时，清空默认值
            if (textBox2.Text == "名称")
            {
                textBox2.Text = "";
            }
        }
        private void textBox2_Leave(object sender, EventArgs e)
        {
            //用户离开时未输入信息，返回默认值
            if (textBox2.Text == "")
            {
                textBox2.Text = "名称";
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox4_Enter(object sender, EventArgs e)
        {
            if (textBox4.Text == "地址")
            {
                textBox4.Text = "";
            }
        }
        private void textBox4_Leave(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
            {
                textBox4.Text = "地址";
            }
        }
        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox5_Enter(object sender, EventArgs e)
        {
            if (textBox5.Text == "支付密码")
            {
                textBox5.Text = "";
            }
        }
        private void textBox5_Leave(object sender, EventArgs e)
        {
            
            string character = textBox5.Text;
            if (textBox5.Text == "")
            {
                textBox5.Text = "支付密码(六位数)";
                
            }
           
             else if (character.Length != 6)
            {
              MessageBox.Show("密码个数不对，请重新输入");
                textBox5.Focus();
                textBox5.SelectAll();
                textBox5.Text = "支付密码(六位数)";
                
            }
           
            
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox6_Enter(object sender, EventArgs e)
        {
            if (textBox6.Text == "密码")
            {
                textBox6.Text = "";
            }
        }
        private void textBox6_Leave(object sender, EventArgs e)
        {
            if (textBox6.Text == "")
            {
                textBox6.Text = "密码";
            }
        }
        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox7_Enter(object sender, EventArgs e)
        {
            if (textBox7.Text == "二次确认密码")
            {
                textBox7.Text = "";
            }
            else if (textBox7.Text != textBox6.Text)
            {
                
                MessageBox.Show("与第一次密码不一致，请重新输入");
                textBox7.Text = "二次确认密码";
            }
        }
        private void textBox7_Leave(object sender, EventArgs e)
        {
            if (textBox7.Text == "")
            {
                textBox7.Text = "二次确认密码";
            }
           

        }
        private void textBox8_Enter(object sender, EventArgs e)
        {

            if (textBox8.Text == "身份证")
            {
                textBox8.Text = "";
            }
        }
        private  void textBox8_Leave(object sender,EventArgs e)
        {
            if (textBox8.Text == "")
            {
                textBox8.Text= "身份证";
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "手机号" || textBox2.Text == "名称" || textBox4.Text == "地址" || textBox6.Text == "密码" || textBox7.Text == "二次确认密码")
            {
                MessageBox.Show("请输入相关信息");
            }




                if (textBox1.Text != "手机号" && textBox2.Text != "名称" && textBox4.Text != "地址" && textBox6.Text != "密码" && textBox7.Text != "二次确认密码")
            {
                try
                {
                    bool reader;
                    String sql = null;

                    //商家注册
                    if (Judge == 0)
                    {
                        sql = "insert into Business(BSname,BSaddress,BStel,BSpwd,BSMoney,BSimage,BSIDcar) values('" + textBox2.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox6.Text + "','0','imag/商家默认头像.jpg','" + textBox8.Text + "')";

                    }
                    //用户注册
                    if (Judge == 1)
                    {
                        sql = "insert into User1(USpwd,USname,UStel,USaddress,pentPwd,USMoney,USimage,USIDcar) values('" + textBox6.Text + "', '" + textBox2.Text + "','" + textBox1.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','0','imag/用户默认头像.jpg','" + textBox8.Text + "')";
                    }
                    reader = DBHelper.GetNonQuery(sql);
                    //判定是否注册成功
                  
                    if (textBox3.Text != button2.Text)
                    {
                        MessageBox.Show("验证码错误，请重新输入");
                        textBox3.Text = "验证码";
                    }
                    else if (reader == true)
                    {
                        MessageBox.Show("注册成功");
                        denglu denglu1 = new denglu();
                        denglu1.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("注册失败,原因不明,自己想");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("程序异常，请联系管理员!\n原因：" + ex.Message);
                }
            }
               
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //选择商家注册时无支付密码，支付密码窗口不可编辑
            Judge = 0;
            this.textBox5.Enabled = false;
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            //选择用户注册时支付密码可用
            Judge = 1;
            this.textBox5.Enabled = true;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox3_Enter(object sender,EventArgs e) 
        {
            if (textBox3.Text == "验证码")
            {
                textBox3.Text = "";
            }
        }
        private void textBox3_leave(object sender,EventArgs e) 
        {
            if (textBox3.Text == "")
            {
                textBox3.Text = "验证码";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //验证码
            Random rd = new Random();
            int nunber = rd.Next(1000, 9999);
            button2.Text = nunber.ToString();
        }

        private void Registration_interface2_Load(object sender, EventArgs e)
        {
            //文字背景透明
            label3.BackColor = Color.Transparent;
            label4.BackColor = Color.Transparent;
            label5.BackColor = Color.Transparent;
            label6.BackColor = Color.Transparent;
            label7.BackColor = Color.Transparent;
            label8.BackColor = Color.Transparent;
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
            radioButton1.BackColor = Color.Transparent;
            radioButton2.BackColor = Color.Transparent;
            checkBox1.BackColor = Color.Transparent;

            //加载锁定支付密码本文框
            this.textBox5.Enabled = false;
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_Enter_1(object sender, EventArgs e)
        {
            if (textBox5.Text == "支付密码(六位数)")
            {
                textBox5.Text = "";
            }
        }

        private void textBox5_Leave_1(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
            {
                textBox5.Text = "支付密码(六位数)";
            }
        }

        private void Registration_interface_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }
    }
}
